package com.edu.chapter06;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class GameAdapter extends RecyclerView.Adapter<GameHolder> {
    private ArrayList<String> data;
    private View view;

    private final OnItemDeleteListener deleteListener;  // 删除回调接口

    // 删除回调接口定义
    public interface OnItemDeleteListener {
        void onDelete(int position);
    }

    //    public GameAdapter(ArrayList<String> data) {
//        this.data = data;
//    }
    public GameAdapter(ArrayList<String> data, OnItemDeleteListener listener) {
        this.data = data;
        this.deleteListener = listener;
    }

    @NonNull
    @Override
    public GameHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 1. 加载布局文件
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_adapter_layout, parent, false);

        // 2. 创建并返回 ViewHolder
//        return new GameHolder(view);

        return new GameHolder(view, deleteListener);
    }

    @Override
    public void onBindViewHolder(@NonNull GameHolder holder, int position) {
        holder.tv_hello.setText(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

}
